var app = getApp();

Page({
  data: {
    id: '',
    thumb: "",
    uniacid: "",
    url: "",
    zhiwei_show: false,
    status_array: ['否', '是'],
    status: 1
  },
  chooseImage: function () {
    var that = this;
    var uniacid = that.data.uniacid
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths[0];
        wx.uploadFile({
          url: that.data.url + 'app/index.php?i=' + uniacid + '&c=entry&a=wxapp&do=Uploadback&m=hyb_yl',
          filePath: tempFilePaths,
          name: 'upfile',
          formData: {},
          success: function (res) {
            console.log(res.data)
            that.setData({
              thumb: res.data
            })
          },
          fail: function (res) {
            console.log(res)
          },
        })
      }
    })
  },
  mapViewTap: function (e) {
    this.setData({
      zhiwei_show: true
    })
  },
  getText: function (a) {
    this.setData({
      text: a.detail.value
    });
  },
  checkboxChange: function (e) {
    var value = e.detail.value;
    var that = this;
    var equipment_arr = that.data.equipment_arr;
    for (var i = 0; i < equipment_arr.length; i++) {
      console.log(value.indexOf(equipment_arr[i].id));
      if (value.indexOf(equipment_arr[i].id) != -1) {
        equipment_arr[i].checked = true
      } else {
        equipment_arr[i].checked = false
      }

    }
    var equipment = "";
    for (var k = 0; k < equipment_arr.length; k++) {
      if (equipment_arr[k].checked) {
        equipment += equipment_arr[k].title + ','
      }
    }
    that.setData({
      equipment_arr: equipment_arr,
      equipment: equipment
    })
  },
  bindstatusChange: function (e) {
    var that = this;
    that.setData({
      status: e.detail.value
    })
  },
  bindclose: function () {
    this.setData({
      zhiwei_show: false
    })
  },
  // 标题输入
  titlechange: function (e) {
    var that = this;
    that.setData({
      title: e.detail.value
    })
  },
  // 金额输入
  moneychange: function (e) {
    var that = this;
    that.setData({
      money: e.detail.value
    })
  },
  // 时长输入
  timechange: function (e) {
    this.setData({
      time_length: e.detail.value
    })
  },
  input: function (e) {
    this.setData({
      content: e.detail.value
    })
  },
  onLoad: function (options) {
    var that = this;
    var id = options.id;
    var uniacid = app.siteInfo.uniacid;
    var bgc = wx.getStorageSync('color')
    that.setData({
      bgc: bgc,

      zid: options.zid
    })
    if (id != undefined) {
      that.setData({
        id: id,
      })
      app.util.request({
        'url': 'entry/wxapp/green.sh_typedetail',
        data: {
          id: id,
        },
        success: function (res) {
          that.setData({
            thumb: res.data.thumb,
            info: res.data,
            content: res.data.content,
            status: res.data.status,
            title: res.data.title,
            money: res.data.money,
            time_length: res.data.time_length,
          })
        },
      })
      
    }
    app.util.request({
      'url':"entry/wxapp/green.imgpath",
      success:function(res)
      {
        that.setData({
          imgpath:res.data.imgpath
        })
      }
    })
    app.util.request({
      'url': "entry/wxapp/green.equipment_arr",
      data: {
        zid: that.data.zid,
        id:that.data.id
      },
      success: function (res) {
        that.setData({
          equipment_arr: res.data,
        })
        var equipment_arr = that.data.equipment_arr
        var equipment = "";
        for (var k = 0; k < equipment_arr.length; k++) {
          if (equipment_arr[k].checked) {
            equipment += equipment_arr[k].title + ','
          }
        }
        that.setData({
          equipment: equipment
        })
      }
    })
    app.util.request({
      'url': 'entry/wxapp/hzbingli.url',
      success: function (res) {
        console.log(res)
        that.setData({
          url: res.data
        })
      },
    })
    that.setData({
      uniacid: uniacid,
    })
  },
  chooseimgpath: function () {
    var that = this;
    var uniacid = that.data.uniacid;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        // tempFilePath可以作为img标签的src属性显示图片
        const tempFilePaths = res.tempFilePaths
        for (var i = 0; i < tempFilePaths.length; i++) {
          wx.uploadFile({
            url: that.data.url + 'app/index.php?i=' + uniacid + '&c=entry&a=wxapp&do=Uploadback&m=hyb_yl',
            filePath: tempFilePaths[i],
            name: 'upfile',
            formData: [],
            success: function (res) {
              that.setData({
                imgpath: res.data,
              })
            }
          })
        }
      }
    })
  },
  bindchangeequipment: function (e) {
    this.setData({
      zhiwei_show: false
    })
  },
  onReady: function () { },
  onShow: function () {

  },
  save: function () {
    var that = this;
    var title = that.data.title;
    var money = that.data.money;
    var time_length = that.data.time_length;
    var status = that.data.status;
    var equipment_arr = that.data.equipment_arr;
    var content = that.data.content;
    var thumb = that.data.thumb;
    var equipment = "";
    for (var i = 0; i < equipment_arr.length; i++) {
      if (equipment_arr[i].checked) {
        equipment += equipment_arr[i].id + ','
      }
    };
    if (title == '') {
      wx.showToast({
        title: '请填写标题',
      })
    } else if (money == '') {
      wx.showToast({
        title: '请填写价格',
      })
    } else if (time_length == '') {
      wx.showToast({
        title: '请填写时长',
      })
    } else {
      app.util.request({
        'url': "entry/wxapp/green.add_shtype",
        data: {
          id: that.data.id,
          title: title,
          money: money,
          thumb: thumb,
          content: content,
          time_length: time_length,
          zid: that.data.zid,
          equipment: equipment,
        },
        success: function (res) {
          wx.showToast({
            title: '编辑成功',
          })
          setTimeout(() => {
            wx.navigateBack({
            })
          }, 500);
        }
      })
    }
  },
});